#include "Mirror.h"

Mirror::Mirror(MirrorMode MM,int width_,int height_)
{
	mm = MM;
	host = 0;
	texture = 0;
	vcam = 0;
	fbo = 0;

	width = width_;
	height = height_;

	fbo = new Effect(width,height);
	if(mm==SMM_PLANE)
		texture = new Texture(TexSize(width,height),3,GL_TEXTURE_2D);
	else
		texture = new Texture(TexSize(width,height),3,GL_TEXTURE_CUBE_MAP);

	vcam = new Camera(width,height);
	vcam->RenderMode(RM_NONE);

	use_oq = 1;
	mirrorslist.push_back(this);
}

Mirror::~Mirror()
{

	mirrorslist.remove(this);
}


void Mirror::Render()
{
	if(host!=0)
	{
		MirrorsShare::currentmirror = this;

		debug::Log<<"mirror entity:"<<host->GetName()<<std::endl;
		
		MirrorsShare::currentmirror = 0;
	}
}

void Mirror::BindEntity(Entity *ent)
{
	host = ent;
	host->BindTex(texture,0);
}

Texture* Mirror::GetTexture()
{
	return texture;
}

void UpdateAllMirrors()
{

	TMirrorList::const_iterator ci = mirrorslist.begin();
	TMirrorList::const_iterator ei = mirrorslist.end();

	for(;ci!=ei;++ci)
	{
		(*ci)->Render();
	}

}